//typedef int TYPE;

typedef struct {    
    int* array;//队列空间
	int rear;//队尾
	int front;//队头
	int N;//空间大小
    int count;//有效元素的个数，判断是否为满
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* mq = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    if(!mq)
      return NULL;
    mq->array = (int*)malloc(sizeof(int)*k);
    mq->N = k;
    mq->count = 0;
    mq -> rear = 0;    
    mq -> front = 0;    
    return mq;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    assert(obj);
    return obj->count == 0;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    assert(obj);
    return obj->count == obj->N;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if(myCircularQueueIsFull(obj))
        return false;
    obj->array[obj->rear++] = value;
    if(obj->rear == obj->N)
        obj->rear = 0;
     obj->count++;
     return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj))
        return false;
    obj->front++;
    obj->front %= obj->N;
    obj->count--;
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj))
        return -1;
    return obj->array[obj->front];
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj))
        return -1;
    return obj->array[(obj->rear - 1 + obj->N) % obj->N];
}


void myCircularQueueFree(MyCircularQueue* obj) {
    assert(obj);
    //由内到外释放
    free(obj->array);
    free(obj);
    return;
}

/**
 * Your MyCircularQueue struct will be instantiated and called as such:
 * MyCircularQueue* obj = myCircularQueueCreate(k);
 * bool param_1 = myCircularQueueEnQueue(obj, value);
 
 * bool param_2 = myCircularQueueDeQueue(obj);
 
 * int param_3 = myCircularQueueFront(obj);
 
 * int param_4 = myCircularQueueRear(obj);
 
 * bool param_5 = myCircularQueueIsEmpty(obj);
 
 * bool param_6 = myCircularQueueIsFull(obj);
 
 * myCircularQueueFree(obj);
*/